import authVal from '../libs/auth/auth'
import { layoutStyle } from '@/libs/layoutStyle';
let handleUtil = {}

/**
 * 数组转化为对象
 * @param data
 * @param fromItem
 * @returns {*}
 */
handleUtil.handleAlleyStratege = (data, fromItem) => {
  if (data.length !== 0) {
    for (let i = 0; i < data.length; i++) {
      let key = data[i].code
      let value = data[i].number
      fromItem[key] = value
    }
  }
  return fromItem
}

/**
 * 处理巷道混放字段对应关系
 * @param data
 * @returns {Array}
 */
handleUtil.handleAlleyData = data => { // data 是对象
  let dataCopy = []
  for (let index in data) {
    if (typeof data[index] !== "undefined") {
      let name = handleUtil.handleByFieldToName(index, 'alleyColumns')
      if (name) {
        dataCopy.push({
          field: name,
          code: index,
          number: String(data[index]),
          type: 'string'
          // type: typeof data[index] === 'number' ? 'number' : 'string'
        })
      }
    }
  }
  return dataCopy
}
/**
 * 处理巷道混放字段对应关系
 * @param data
 * @returns {Array}
 */
// 根据字段名返回对应中文名
handleUtil.handleByFieldToName = (data, type) => {
  let feildList = authVal[type]
  for (let i = 0; i < feildList.length; i++) {
    if (feildList[i].code === data) {
      return feildList[i].field
    }
  }
}

/**
 * 处理上架策略表字段对应关系(也可以用任务下发)
 */
handleUtil.handleUpShelfColumns = (data, columnKey) => {
  let columns = []
  let dataModal = []
  for (let index in data) {
    let title = columnKey[index]
    if (title) {
      let width = title.length * 20+20
      columns.push({
        title: title + ' %',
        key: index,
        align: 'center',
        width: width,
        formatter: function (row, column) {
          return row[index] + '%'
        }
      })
      dataModal.push({
        field: title,
        number: data[index],
        code: index
      })
    }
  }
  return {columns: columns, dataModal: dataModal}
}
/**
 * 处理自动调车策略表字段对应关系(也可以用于下架策略,合托策略)
 */
handleUtil.autoDispatchCarColumns = (data, columnKey) => {
  let columns = []
  let dataModal = []
  for (let index in data) {
    let title = columnKey[index]
    if (title) {
      let width = title.length * 25
      columns.push({
        title: title,
        key: index,
        align: 'center',
        width: width,
        formatter: function (row, column) {
          return row[index]
        }
      })
      dataModal.push({
        field: title,
        number: data[index],
        code: index,
        isSelect:title==="是否需要人工确认"||title==="是否允许工作站空闲"?true:false,
      })

    }
  }
  return {columns: columns, dataModal: dataModal}
}

/**
 * 数组转化为对象
 */
handleUtil.arrayToObject = data => {
  let body = {}
  if (data.length !== 0) {
    for (let i = 0; i < data.length; i++) {
      let key = data[i].code
      let value = data[i].number
      body[key] = value
    }
  }
  return body
}
/**
 * 数组对象转String数组
 */
handleUtil.arrayObjectToArray = data => {
  let dataList = []
  for (let i = 0; i < data.length; i++) {
    dataList.push(data[i].number)
  }
  return dataList
}
/**
 * 解析楼层和仓间占比数据
 */
handleUtil.analysisFloorData = (data, val) => {
  let dataList = []
  let index = 1
  if (data instanceof Array) {
    for (let i = 0; i < data.length; i++) {
      if (val === 'floor') {
        dataList.push({
          field: (i + 1) + '楼',
          number: data[i]
        })
      } else {
        let name = ''
        if ((i + 1) % 2 === 0) {
          name = '左'
        } else {
          name = '右'
        }
        dataList.push({
          field: index + '楼' + name + '仓间',
          number: data[i]
        })
        if ((i + 1) % 2 === 0) {
          index += 1
        }
      }
    }
  } else {
    console.log('数据源不接收数组之外数据');
  }
  return dataList
}

/**
 * 打印
 */
handleUtil.title = function (title) {
  title = title || '智能仓储管理系统';
  window.document.title = title;
};
handleUtil.printPage = function (id, css, title) {
  // 设置打印时候的标题
  handleUtil.title(title);
  let iFrame = document.createElement('IFRAME');
  iFrame.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;font-weight:400');
  document.body.appendChild(iFrame);
  let doc = iFrame.contentWindow.document;
  let content = document.getElementById(id).innerHTML;
  // console.log(content + css + layoutStyle);
  doc.write(css);
  doc.write(layoutStyle);
  doc.write(content);
  // doc.write(content + css + layoutStyle);
  doc.close();
  iFrame.contentWindow.focus();
  iFrame.onload = function () { //解决图片显示不了的问题
    iFrame.contentWindow.print();
    document.body.removeChild(iFrame);
  };
  // 重置标题为默认
  handleUtil.title();
};


















export default handleUtil;
